using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Datos
{
    public class Transaccion : DBAccess
    {
        private readonly SqlTransaction Trans;

        public Transaccion()
        {
            Conectar();

            Trans = cn.BeginTransaction();
        }

        public void AddCommand(string spname, Hashtable parametros)
        {
            SqlCommand cm = new SqlCommand();
            cm.Transaction = Trans;
            cm.Connection = cn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = spname;

            foreach (DictionaryEntry Param in parametros)
            {
                cm.Parameters.AddWithValue(Param.Key.ToString(), Param.Value);
            }

            cm.ExecuteNonQuery();
        }

        public bool ExecuteTransaction()
        {                
            bool Result = false;
            
            try
            {
                Trans.Commit();
                Result = true;
            }
            catch (SqlException ex)
            {
                Trans.Rollback();

                //Logger.LogError(ex.Message, this);
            }

            return Result;
        }
    }
}
